
<!doctype html>
<html lang="en" class="no-js">
  <head>
    
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width,initial-scale=1">
      
      
      
      
        <link rel="prev" href="../03_Javascript%E9%AB%98%E7%BA%A7%E5%AE%9E%E9%AA%8C/index.html">
      
      
        <link rel="next" href="../03_Bootstrap/index.html">
      
      <link rel="icon" href="../assets/images/favicon.png">
      <meta name="generator" content="mkdocs-1.4.2, mkdocs-material-9.1.3">
    
    
      
        <title>03 jQuery - 基于WEB的应用程序开发-实验指导书</title>
      
    
    
      <link rel="stylesheet" href="../assets/stylesheets/main.c4a75a56.min.css">
      
        
        <link rel="stylesheet" href="../assets/stylesheets/palette.a0c5b2b5.min.css">
      
      

    
    
    
      
        
        
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
        <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,700,700i%7CRoboto+Mono:400,400i,700,700i&display=fallback">
        <style>:root{--md-text-font:"Roboto";--md-code-font:"Roboto Mono"}</style>
      
    
    
      <link rel="stylesheet" href="../assets/stylesheets/extra.css">
    
    <script>__md_scope=new URL("..",location),__md_hash=e=>[...e].reduce((e,_)=>(e<<5)-e+_.charCodeAt(0),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script>
    
      

    
    
    
  </head>
  
  
    
    
      
    
    
    
    
    <body dir="ltr" data-md-color-scheme="default" data-md-color-primary="purple" data-md-color-accent="">
  
    
    
      <script>var palette=__md_get("__palette");if(palette&&"object"==typeof palette.color)for(var key of Object.keys(palette.color))document.body.setAttribute("data-md-color-"+key,palette.color[key])</script>
    
    <input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer" autocomplete="off">
    <input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off">
    <label class="md-overlay" for="__drawer"></label>
    <div data-md-component="skip">
      
        
        <a href="#jquery" class="md-skip">
          Skip to content
        </a>
      
    </div>
    <div data-md-component="announce">
      
    </div>
    
    
      

  

<header class="md-header md-header--shadow" data-md-component="header">
  <nav class="md-header__inner md-grid" aria-label="Header">
    <a href=".." title="基于WEB的应用程序开发-实验指导书" class="md-header__button md-logo" aria-label="基于WEB的应用程序开发-实验指导书" data-md-component="logo">
      
  
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54Z"/></svg>

    </a>
    <label class="md-header__button md-icon" for="__drawer">
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M3 6h18v2H3V6m0 5h18v2H3v-2m0 5h18v2H3v-2Z"/></svg>
    </label>
    <div class="md-header__title" data-md-component="header-title">
      <div class="md-header__ellipsis">
        <div class="md-header__topic">
          <span class="md-ellipsis">
            基于WEB的应用程序开发-实验指导书
          </span>
        </div>
        <div class="md-header__topic" data-md-component="header-topic">
          <span class="md-ellipsis">
            
              03 jQuery
            
          </span>
        </div>
      </div>
    </div>
    
      <form class="md-header__option" data-md-component="palette">
        
          
          <input class="md-option" data-md-color-media="" data-md-color-scheme="default" data-md-color-primary="purple" data-md-color-accent=""  aria-label="夜间模式"  type="radio" name="__palette" id="__palette_1">
          
            <label class="md-header__button md-icon" title="夜间模式" for="__palette_2" hidden>
              <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 7a5 5 0 0 1 5 5 5 5 0 0 1-5 5 5 5 0 0 1-5-5 5 5 0 0 1 5-5m0 2a3 3 0 0 0-3 3 3 3 0 0 0 3 3 3 3 0 0 0 3-3 3 3 0 0 0-3-3m0-7 2.39 3.42C13.65 5.15 12.84 5 12 5c-.84 0-1.65.15-2.39.42L12 2M3.34 7l4.16-.35A7.2 7.2 0 0 0 5.94 8.5c-.44.74-.69 1.5-.83 2.29L3.34 7m.02 10 1.76-3.77a7.131 7.131 0 0 0 2.38 4.14L3.36 17M20.65 7l-1.77 3.79a7.023 7.023 0 0 0-2.38-4.15l4.15.36m-.01 10-4.14.36c.59-.51 1.12-1.14 1.54-1.86.42-.73.69-1.5.83-2.29L20.64 17M12 22l-2.41-3.44c.74.27 1.55.44 2.41.44.82 0 1.63-.17 2.37-.44L12 22Z"/></svg>
            </label>
          
        
          
          <input class="md-option" data-md-color-media="" data-md-color-scheme="slate" data-md-color-primary="teal" data-md-color-accent=""  aria-label="日间模式"  type="radio" name="__palette" id="__palette_2">
          
            <label class="md-header__button md-icon" title="日间模式" for="__palette_1" hidden>
              <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="m17.75 4.09-2.53 1.94.91 3.06-2.63-1.81-2.63 1.81.91-3.06-2.53-1.94L12.44 4l1.06-3 1.06 3 3.19.09m3.5 6.91-1.64 1.25.59 1.98-1.7-1.17-1.7 1.17.59-1.98L15.75 11l2.06-.05L18.5 9l.69 1.95 2.06.05m-2.28 4.95c.83-.08 1.72 1.1 1.19 1.85-.32.45-.66.87-1.08 1.27C15.17 23 8.84 23 4.94 19.07c-3.91-3.9-3.91-10.24 0-14.14.4-.4.82-.76 1.27-1.08.75-.53 1.93.36 1.85 1.19-.27 2.86.69 5.83 2.89 8.02a9.96 9.96 0 0 0 8.02 2.89m-1.64 2.02a12.08 12.08 0 0 1-7.8-3.47c-2.17-2.19-3.33-5-3.49-7.82-2.81 3.14-2.7 7.96.31 10.98 3.02 3.01 7.84 3.12 10.98.31Z"/></svg>
            </label>
          
        
      </form>
    
    
    
    
  </nav>
  
</header>
    
    <div class="md-container" data-md-component="container">
      
      
        
          
        
      
      <main class="md-main" data-md-component="main">
        <div class="md-main__inner md-grid">
          
            
              
              <div class="md-sidebar md-sidebar--primary" data-md-component="sidebar" data-md-type="navigation" >
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">
                    


<nav class="md-nav md-nav--primary" aria-label="Navigation" data-md-level="0">
  <label class="md-nav__title" for="__drawer">
    <a href=".." title="基于WEB的应用程序开发-实验指导书" class="md-nav__button md-logo" aria-label="基于WEB的应用程序开发-实验指导书" data-md-component="logo">
      
  
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54Z"/></svg>

    </a>
    基于WEB的应用程序开发-实验指导书
  </label>
  
  <ul class="md-nav__list" data-md-scrollfix>
    
      
      
      

  
  
  
    <li class="md-nav__item">
      <a href="../03_Javascript%E5%9F%BA%E7%A1%80/index.html" class="md-nav__link">
        03 Javascript基础
      </a>
    </li>
  

    
      
      
      

  
  
  
    <li class="md-nav__item">
      <a href="../03_Javascript%E9%AB%98%E7%BA%A7%E5%AE%9E%E9%AA%8C/index.html" class="md-nav__link">
        03 Javascript高级实验
      </a>
    </li>
  

    
      
      
      

  
  
    
  
  
    <li class="md-nav__item md-nav__item--active">
      
      <input class="md-nav__toggle md-toggle" type="checkbox" id="__toc">
      
      
        
      
      
        <label class="md-nav__link md-nav__link--active" for="__toc">
          03 jQuery
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <a href="index.html" class="md-nav__link md-nav__link--active">
        03 jQuery
      </a>
      
        

  

<nav class="md-nav md-nav--secondary" aria-label=" ">
  
  
  
    
  
  
    <label class="md-nav__title" for="__toc">
      <span class="md-nav__icon md-icon"></span>
       
    </label>
    <ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
      
        <li class="md-nav__item">
  <a href="#1" class="md-nav__link">
    1 实验类型
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#2" class="md-nav__link">
    2 实验目的
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#3" class="md-nav__link">
    3 实验内容与要求
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#4" class="md-nav__link">
    4 实验环境
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#5" class="md-nav__link">
    5 步骤
  </a>
  
    <nav class="md-nav" aria-label="5 步骤">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_1" class="md-nav__link">
    选择器
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_2" class="md-nav__link">
    事件
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_3" class="md-nav__link">
    效果
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_4" class="md-nav__link">
    遍历
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#html" class="md-nav__link">
    Html
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#ajaxpython" class="md-nav__link">
    Ajax(Python)
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
      
    </ul>
  
</nav>
      
    </li>
  

    
      
      
      

  
  
  
    <li class="md-nav__item">
      <a href="../03_Bootstrap/index.html" class="md-nav__link">
        03 Bootstrap
      </a>
    </li>
  

    
      
      
      

  
  
  
    <li class="md-nav__item">
      <a href="../03_Vue.js%E5%9F%BA%E7%A1%80/index.html" class="md-nav__link">
        03 Vue.js
      </a>
    </li>
  

    
  </ul>
</nav>
                  </div>
                </div>
              </div>
            
            
              
              <div class="md-sidebar md-sidebar--secondary" data-md-component="sidebar" data-md-type="toc" >
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">
                    

  

<nav class="md-nav md-nav--secondary" aria-label=" ">
  
  
  
    
  
  
    <label class="md-nav__title" for="__toc">
      <span class="md-nav__icon md-icon"></span>
       
    </label>
    <ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
      
        <li class="md-nav__item">
  <a href="#1" class="md-nav__link">
    1 实验类型
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#2" class="md-nav__link">
    2 实验目的
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#3" class="md-nav__link">
    3 实验内容与要求
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#4" class="md-nav__link">
    4 实验环境
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#5" class="md-nav__link">
    5 步骤
  </a>
  
    <nav class="md-nav" aria-label="5 步骤">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_1" class="md-nav__link">
    选择器
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_2" class="md-nav__link">
    事件
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_3" class="md-nav__link">
    效果
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_4" class="md-nav__link">
    遍历
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#html" class="md-nav__link">
    Html
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#ajaxpython" class="md-nav__link">
    Ajax(Python)
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
      
    </ul>
  
</nav>
                  </div>
                </div>
              </div>
            
          
          
            <div class="md-content" data-md-component="content">
              <article class="md-content__inner md-typeset">
                
                  


<h1 id="jquery">jQuery实验</h1>
<h2 id="1">1 实验类型</h2>
<p>验证型，2学时，必选实验</p>
<h2 id="2">2 实验目的</h2>
<p>掌握jQuery常见选择器；掌握jQuery常见事件；掌握jQuery常见效果；掌握jQuery对象遍历方法；掌握jQuery操作Html对象；熟悉jQuery Ajax方法。</p>
<h2 id="3">3 实验内容与要求</h2>
<p>实现jQuery常见选择器的使用；实现jQuery常见事件处理；实现jQuery常见效果；实现jQuery对象遍历方法；实现jQuery操作Html对象；通过FastApi实现jQuery Ajax请求与响应。</p>
<h2 id="4">4 实验环境</h2>
<p><code>Microsoft Edge/Chrome/Firefox</code>等浏览器，<code>Visual Studio Code</code>，<code>REST Client</code>，<code>Python 3.4+</code>，<code>FastAPI</code>，<code>Uvicorn</code></p>
<h2 id="5">5 步骤</h2>
<blockquote>
<p>工作目录：<code>C:/${学号}</code>， 后续文件均存放在工作目录</p>
</blockquote>
<h3 id="_1">选择器</h3>
<p>创建一个段落元素(<code>id=p1</code>)，通过jQuery的<em>元素选择器</em>,<em>#id选择器</em>,<em>.class选择器</em>,<em>CSS选择器</em>等获取<code>p1</code>并实现显示/隐藏功能。</p>
<ol>
<li>
<p>编写Html(<code>jq_selector.html</code>)代码，分别实现<em>元素选择器</em>,<em>#id选择器</em>,<em>.class选择器</em>,<em>CSS选择器</em>，参考代码如下：</p>
<p><!--<iframe src="./src/jq_selector.html" frameborder="0" width="100%" height="300"></iframe>-->
<div class="highlight"><table class="highlighttable"><tr><th colspan="2" class="filename"><span class="filename">jq_selector.html</span></th></tr><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"><a href="#__codelineno-0-1"> 1</a></span>
<span class="normal"><a href="#__codelineno-0-2"> 2</a></span>
<span class="normal"><a href="#__codelineno-0-3"> 3</a></span>
<span class="normal"><a href="#__codelineno-0-4"> 4</a></span>
<span class="normal"><a href="#__codelineno-0-5"> 5</a></span>
<span class="normal"><a href="#__codelineno-0-6"> 6</a></span>
<span class="normal"><a href="#__codelineno-0-7"> 7</a></span>
<span class="normal"><a href="#__codelineno-0-8"> 8</a></span>
<span class="normal"><a href="#__codelineno-0-9"> 9</a></span>
<span class="normal"><a href="#__codelineno-0-10">10</a></span>
<span class="normal"><a href="#__codelineno-0-11">11</a></span>
<span class="normal"><a href="#__codelineno-0-12">12</a></span>
<span class="normal"><a href="#__codelineno-0-13">13</a></span>
<span class="normal"><a href="#__codelineno-0-14">14</a></span>
<span class="normal"><a href="#__codelineno-0-15">15</a></span>
<span class="normal"><a href="#__codelineno-0-16">16</a></span>
<span class="normal"><a href="#__codelineno-0-17">17</a></span>
<span class="normal"><a href="#__codelineno-0-18">18</a></span>
<span class="normal"><a href="#__codelineno-0-19">19</a></span>
<span class="normal"><a href="#__codelineno-0-20">20</a></span>
<span class="normal"><a href="#__codelineno-0-21">21</a></span>
<span class="normal"><a href="#__codelineno-0-22">22</a></span>
<span class="normal"><a href="#__codelineno-0-23">23</a></span>
<span class="normal"><a href="#__codelineno-0-24">24</a></span>
<span class="normal"><a href="#__codelineno-0-25">25</a></span>
<span class="normal"><a href="#__codelineno-0-26">26</a></span>
<span class="normal"><a href="#__codelineno-0-27">27</a></span>
<span class="normal"><a href="#__codelineno-0-28">28</a></span>
<span class="normal"><a href="#__codelineno-0-29">29</a></span>
<span class="normal"><a href="#__codelineno-0-30">30</a></span>
<span class="normal"><a href="#__codelineno-0-31">31</a></span></pre></div></td><td class="code"><div><pre><span></span><code><a id="__codelineno-0-1" name="__codelineno-0-1"></a><span class="cm">&lt;!-- selector.html --&gt;</span>
<a id="__codelineno-0-2" name="__codelineno-0-2"></a><span class="cp">&lt;!DOCTYPE html&gt;</span>
<a id="__codelineno-0-3" name="__codelineno-0-3"></a><span class="p">&lt;</span><span class="nt">html</span><span class="p">&gt;</span>
<a id="__codelineno-0-4" name="__codelineno-0-4"></a>
<a id="__codelineno-0-5" name="__codelineno-0-5"></a><span class="p">&lt;</span><span class="nt">head</span><span class="p">&gt;</span>
<a id="__codelineno-0-6" name="__codelineno-0-6"></a>  <span class="p">&lt;</span><span class="nt">meta</span> <span class="na">charset</span><span class="o">=</span><span class="s">&quot;utf-8&quot;</span><span class="p">&gt;</span>
<a id="__codelineno-0-7" name="__codelineno-0-7"></a>  <span class="p">&lt;</span><span class="nt">title</span><span class="p">&gt;</span>选择器示例<span class="p">&lt;/</span><span class="nt">title</span><span class="p">&gt;</span>
<a id="__codelineno-0-8" name="__codelineno-0-8"></a>  <span class="p">&lt;</span><span class="nt">script</span> <span class="na">src</span><span class="o">=</span><span class="s">&quot;jquery-3.6.1.min.js&quot;</span><span class="p">&gt;&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
<a id="__codelineno-0-9" name="__codelineno-0-9"></a><span class="p">&lt;/</span><span class="nt">head</span><span class="p">&gt;</span>
<a id="__codelineno-0-10" name="__codelineno-0-10"></a>
<a id="__codelineno-0-11" name="__codelineno-0-11"></a><span class="p">&lt;</span><span class="nt">body</span><span class="p">&gt;</span>
<a id="__codelineno-0-12" name="__codelineno-0-12"></a>  <span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;</span>元素选择器<span class="p">&lt;/</span><span class="nt">h1</span><span class="p">&gt;</span>
<a id="__codelineno-0-13" name="__codelineno-0-13"></a>  <span class="p">&lt;</span><span class="nt">p</span> <span class="na">id</span><span class="o">=</span><span class="s">&quot;p1&quot;</span> <span class="na">class</span><span class="o">=</span><span class="s">&quot;my_class&quot;</span> <span class="na">style</span><span class="o">=</span><span class="s">&quot;background-color: green;&quot;</span><span class="p">&gt;</span>这是一个段落。<span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
<a id="__codelineno-0-14" name="__codelineno-0-14"></a>
<a id="__codelineno-0-15" name="__codelineno-0-15"></a>  <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onclick</span><span class="o">=</span><span class="s">&quot;$(&#39;p&#39;).show()&quot;</span><span class="p">&gt;</span>显示段落<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
<a id="__codelineno-0-16" name="__codelineno-0-16"></a>  <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onclick</span><span class="o">=</span><span class="s">&quot;$(&#39;p&#39;).hide()&quot;</span><span class="p">&gt;</span>隐藏段落<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
<a id="__codelineno-0-17" name="__codelineno-0-17"></a>
<a id="__codelineno-0-18" name="__codelineno-0-18"></a>  <span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;</span>#id选择器<span class="p">&lt;/</span><span class="nt">h1</span><span class="p">&gt;</span>
<a id="__codelineno-0-19" name="__codelineno-0-19"></a>  <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onclick</span><span class="o">=</span><span class="s">&quot;$(&#39;#p1&#39;).show()&quot;</span><span class="p">&gt;</span>显示段落1<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
<a id="__codelineno-0-20" name="__codelineno-0-20"></a>  <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onclick</span><span class="o">=</span><span class="s">&quot;$(&#39;#p1&#39;).hide()&quot;</span><span class="p">&gt;</span>隐藏段落1<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
<a id="__codelineno-0-21" name="__codelineno-0-21"></a>
<a id="__codelineno-0-22" name="__codelineno-0-22"></a>  <span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;</span>.class选择器<span class="p">&lt;/</span><span class="nt">h1</span><span class="p">&gt;</span>
<a id="__codelineno-0-23" name="__codelineno-0-23"></a>  <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onclick</span><span class="o">=</span><span class="s">&quot;$(&#39;.my_class&#39;).show()&quot;</span><span class="p">&gt;</span>显示段落my_class<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
<a id="__codelineno-0-24" name="__codelineno-0-24"></a>  <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onclick</span><span class="o">=</span><span class="s">&quot;$(&#39;.my_class&#39;).hide()&quot;</span><span class="p">&gt;</span>隐藏段落my_class<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
<a id="__codelineno-0-25" name="__codelineno-0-25"></a>
<a id="__codelineno-0-26" name="__codelineno-0-26"></a>  <span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;</span>CSS选择器<span class="p">&lt;/</span><span class="nt">h1</span><span class="p">&gt;</span>
<a id="__codelineno-0-27" name="__codelineno-0-27"></a>  <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onclick</span><span class="o">=</span><span class="s">&quot;$(&#39;p&#39;).css(&#39;background-color&#39;, &#39;red&#39;)&quot;</span><span class="p">&gt;</span>修改段落背景为红色<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
<a id="__codelineno-0-28" name="__codelineno-0-28"></a>  <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onclick</span><span class="o">=</span><span class="s">&quot;$(&#39;p&#39;).css(&#39;background-color&#39;, &#39;blue&#39;)&quot;</span><span class="p">&gt;</span>修改段落背景为蓝色<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
<a id="__codelineno-0-29" name="__codelineno-0-29"></a><span class="p">&lt;/</span><span class="nt">body</span><span class="p">&gt;</span>
<a id="__codelineno-0-30" name="__codelineno-0-30"></a>
<a id="__codelineno-0-31" name="__codelineno-0-31"></a><span class="p">&lt;/</span><span class="nt">html</span><span class="p">&gt;</span>
</code></pre></div></td></tr></table></div></p>
</li>
<li>
<p>在浏览器中打开页面，操作各模块并观察效果</p>
</li>
</ol>
<h3 id="_2">事件</h3>
<p>编写段落元素(<code>id=p1</code>)，通过jQuery捕获<code>p1</code>的<em>click</em>事件；编写文本框(id=k1)，通过jQuery捕获<code>k1</code>的<em>keypress</em>事件；编写表单(id=f1)，捕获<code>f1</code>的<em>submit</em>事件；捕获<em>window</em>对象的<em>resize</em>事件。</p>
<ol>
<li>
<p>编写Html代码，分别实现<em>鼠标事件</em>,<em>键盘事件</em>,<em>表单事件</em>,<em>文档/窗口事件</em>，参考代码如下：</p>
<p><!--<iframe src="./src/jq_event.html" frameborder="0" width="100%" height="300"></iframe>-->
<div class="highlight"><table class="highlighttable"><tr><th colspan="2" class="filename"><span class="filename">jq_event.html</span></th></tr><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"><a href="#__codelineno-1-1"> 1</a></span>
<span class="normal"><a href="#__codelineno-1-2"> 2</a></span>
<span class="normal"><a href="#__codelineno-1-3"> 3</a></span>
<span class="normal"><a href="#__codelineno-1-4"> 4</a></span>
<span class="normal"><a href="#__codelineno-1-5"> 5</a></span>
<span class="normal"><a href="#__codelineno-1-6"> 6</a></span>
<span class="normal"><a href="#__codelineno-1-7"> 7</a></span>
<span class="normal"><a href="#__codelineno-1-8"> 8</a></span>
<span class="normal"><a href="#__codelineno-1-9"> 9</a></span>
<span class="normal"><a href="#__codelineno-1-10">10</a></span>
<span class="normal"><a href="#__codelineno-1-11">11</a></span>
<span class="normal"><a href="#__codelineno-1-12">12</a></span>
<span class="normal"><a href="#__codelineno-1-13">13</a></span>
<span class="normal"><a href="#__codelineno-1-14">14</a></span>
<span class="normal"><a href="#__codelineno-1-15">15</a></span>
<span class="normal"><a href="#__codelineno-1-16">16</a></span>
<span class="normal"><a href="#__codelineno-1-17">17</a></span>
<span class="normal"><a href="#__codelineno-1-18">18</a></span>
<span class="normal"><a href="#__codelineno-1-19">19</a></span>
<span class="normal"><a href="#__codelineno-1-20">20</a></span>
<span class="normal"><a href="#__codelineno-1-21">21</a></span>
<span class="normal"><a href="#__codelineno-1-22">22</a></span>
<span class="normal"><a href="#__codelineno-1-23">23</a></span>
<span class="normal"><a href="#__codelineno-1-24">24</a></span>
<span class="normal"><a href="#__codelineno-1-25">25</a></span>
<span class="normal"><a href="#__codelineno-1-26">26</a></span>
<span class="normal"><a href="#__codelineno-1-27">27</a></span>
<span class="normal"><a href="#__codelineno-1-28">28</a></span>
<span class="normal"><a href="#__codelineno-1-29">29</a></span>
<span class="normal"><a href="#__codelineno-1-30">30</a></span>
<span class="normal"><a href="#__codelineno-1-31">31</a></span>
<span class="normal"><a href="#__codelineno-1-32">32</a></span>
<span class="normal"><a href="#__codelineno-1-33">33</a></span>
<span class="normal"><a href="#__codelineno-1-34">34</a></span>
<span class="normal"><a href="#__codelineno-1-35">35</a></span>
<span class="normal"><a href="#__codelineno-1-36">36</a></span>
<span class="normal"><a href="#__codelineno-1-37">37</a></span>
<span class="normal"><a href="#__codelineno-1-38">38</a></span>
<span class="normal"><a href="#__codelineno-1-39">39</a></span>
<span class="normal"><a href="#__codelineno-1-40">40</a></span>
<span class="normal"><a href="#__codelineno-1-41">41</a></span>
<span class="normal"><a href="#__codelineno-1-42">42</a></span>
<span class="normal"><a href="#__codelineno-1-43">43</a></span>
<span class="normal"><a href="#__codelineno-1-44">44</a></span>
<span class="normal"><a href="#__codelineno-1-45">45</a></span>
<span class="normal"><a href="#__codelineno-1-46">46</a></span>
<span class="normal"><a href="#__codelineno-1-47">47</a></span>
<span class="normal"><a href="#__codelineno-1-48">48</a></span>
<span class="normal"><a href="#__codelineno-1-49">49</a></span>
<span class="normal"><a href="#__codelineno-1-50">50</a></span></pre></div></td><td class="code"><div><pre><span></span><code><a id="__codelineno-1-1" name="__codelineno-1-1"></a><span class="cm">&lt;!-- event.html --&gt;</span>
<a id="__codelineno-1-2" name="__codelineno-1-2"></a><span class="cp">&lt;!DOCTYPE html&gt;</span>
<a id="__codelineno-1-3" name="__codelineno-1-3"></a><span class="p">&lt;</span><span class="nt">html</span><span class="p">&gt;</span>
<a id="__codelineno-1-4" name="__codelineno-1-4"></a>
<a id="__codelineno-1-5" name="__codelineno-1-5"></a><span class="p">&lt;</span><span class="nt">head</span><span class="p">&gt;</span>
<a id="__codelineno-1-6" name="__codelineno-1-6"></a>  <span class="p">&lt;</span><span class="nt">meta</span> <span class="na">charset</span><span class="o">=</span><span class="s">&quot;utf-8&quot;</span><span class="p">&gt;</span>
<a id="__codelineno-1-7" name="__codelineno-1-7"></a>  <span class="p">&lt;</span><span class="nt">title</span><span class="p">&gt;</span>事件示例<span class="p">&lt;/</span><span class="nt">title</span><span class="p">&gt;</span>
<a id="__codelineno-1-8" name="__codelineno-1-8"></a>  <span class="p">&lt;</span><span class="nt">script</span> <span class="na">src</span><span class="o">=</span><span class="s">&quot;jquery-3.6.1.min.js&quot;</span><span class="p">&gt;&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
<a id="__codelineno-1-9" name="__codelineno-1-9"></a>  <span class="p">&lt;</span><span class="nt">script</span><span class="p">&gt;</span>
<a id="__codelineno-1-10" name="__codelineno-1-10"></a><span class="w">    </span><span class="nx">$</span><span class="p">(</span><span class="nb">document</span><span class="p">).</span><span class="nx">ready</span><span class="p">(</span><span class="kd">function</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-1-11" name="__codelineno-1-11"></a><span class="w">      </span><span class="nx">$action_docment</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">$</span><span class="p">(</span><span class="s2">&quot;#action_docment&quot;</span><span class="p">)</span>
<a id="__codelineno-1-12" name="__codelineno-1-12"></a><span class="w">      </span><span class="nx">$</span><span class="p">(</span><span class="s2">&quot;#p1&quot;</span><span class="p">).</span><span class="nx">click</span><span class="p">(</span><span class="kd">function</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-1-13" name="__codelineno-1-13"></a><span class="w">        </span><span class="nx">$</span><span class="p">(</span><span class="s2">&quot;#action_mouse&quot;</span><span class="p">).</span><span class="nx">text</span><span class="p">(</span><span class="s2">&quot;click事件&quot;</span><span class="p">);</span>
<a id="__codelineno-1-14" name="__codelineno-1-14"></a><span class="w">      </span><span class="p">});</span>
<a id="__codelineno-1-15" name="__codelineno-1-15"></a><span class="w">      </span><span class="nx">$</span><span class="p">(</span><span class="s2">&quot;#k1&quot;</span><span class="p">).</span><span class="nx">keypress</span><span class="p">(</span><span class="kd">function</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-1-16" name="__codelineno-1-16"></a><span class="w">        </span><span class="nx">$</span><span class="p">(</span><span class="s2">&quot;#action_key&quot;</span><span class="p">).</span><span class="nx">text</span><span class="p">(</span><span class="s2">&quot;keypress事件&quot;</span><span class="p">);</span>
<a id="__codelineno-1-17" name="__codelineno-1-17"></a><span class="w">      </span><span class="p">});</span>
<a id="__codelineno-1-18" name="__codelineno-1-18"></a><span class="w">      </span><span class="nx">$</span><span class="p">(</span><span class="s2">&quot;#f1&quot;</span><span class="p">).</span><span class="nx">submit</span><span class="p">(</span><span class="kd">function</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-1-19" name="__codelineno-1-19"></a><span class="w">        </span><span class="nx">$</span><span class="p">(</span><span class="s2">&quot;#action_form&quot;</span><span class="p">).</span><span class="nx">text</span><span class="p">(</span><span class="s2">&quot;submit事件&quot;</span><span class="p">);</span>
<a id="__codelineno-1-20" name="__codelineno-1-20"></a><span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="kc">false</span><span class="p">;</span>
<a id="__codelineno-1-21" name="__codelineno-1-21"></a><span class="w">      </span><span class="p">});</span>
<a id="__codelineno-1-22" name="__codelineno-1-22"></a><span class="w">      </span><span class="nx">$</span><span class="p">(</span><span class="nb">window</span><span class="p">).</span><span class="nx">resize</span><span class="p">(</span><span class="kd">function</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-1-23" name="__codelineno-1-23"></a><span class="w">        </span><span class="nx">$</span><span class="p">(</span><span class="s2">&quot;#action_docment&quot;</span><span class="p">).</span><span class="nx">text</span><span class="p">(</span><span class="s2">&quot;resize事件&quot;</span><span class="p">)</span>
<a id="__codelineno-1-24" name="__codelineno-1-24"></a><span class="w">      </span><span class="p">})</span>
<a id="__codelineno-1-25" name="__codelineno-1-25"></a><span class="w">    </span><span class="p">});</span>
<a id="__codelineno-1-26" name="__codelineno-1-26"></a><span class="w">  </span><span class="p">&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
<a id="__codelineno-1-27" name="__codelineno-1-27"></a><span class="p">&lt;/</span><span class="nt">head</span><span class="p">&gt;</span>
<a id="__codelineno-1-28" name="__codelineno-1-28"></a>
<a id="__codelineno-1-29" name="__codelineno-1-29"></a><span class="p">&lt;</span><span class="nt">body</span><span class="p">&gt;</span>
<a id="__codelineno-1-30" name="__codelineno-1-30"></a>
<a id="__codelineno-1-31" name="__codelineno-1-31"></a>  <span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;</span>鼠标事件<span class="p">&lt;/</span><span class="nt">h1</span><span class="p">&gt;</span>
<a id="__codelineno-1-32" name="__codelineno-1-32"></a>  <span class="p">&lt;</span><span class="nt">span</span><span class="p">&gt;</span>当前动作：<span class="p">&lt;/</span><span class="nt">span</span><span class="p">&gt;&lt;</span><span class="nt">span</span> <span class="na">id</span><span class="o">=</span><span class="s">&quot;action_mouse&quot;</span><span class="p">&gt;&lt;/</span><span class="nt">span</span><span class="p">&gt;</span>
<a id="__codelineno-1-33" name="__codelineno-1-33"></a>  <span class="p">&lt;</span><span class="nt">p</span> <span class="na">id</span><span class="o">=</span><span class="s">&quot;p1&quot;</span><span class="p">&gt;</span>这是一个段落。<span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
<a id="__codelineno-1-34" name="__codelineno-1-34"></a>
<a id="__codelineno-1-35" name="__codelineno-1-35"></a>  <span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;</span>键盘事件<span class="p">&lt;/</span><span class="nt">h1</span><span class="p">&gt;</span>
<a id="__codelineno-1-36" name="__codelineno-1-36"></a>  <span class="p">&lt;</span><span class="nt">span</span><span class="p">&gt;</span>当前动作：<span class="p">&lt;/</span><span class="nt">span</span><span class="p">&gt;&lt;</span><span class="nt">span</span> <span class="na">id</span><span class="o">=</span><span class="s">&quot;action_key&quot;</span><span class="p">&gt;&lt;/</span><span class="nt">span</span><span class="p">&gt;</span>
<a id="__codelineno-1-37" name="__codelineno-1-37"></a>  <span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;&lt;</span><span class="nt">input</span> <span class="na">id</span><span class="o">=</span><span class="s">&quot;k1&quot;</span> <span class="na">type</span><span class="o">=</span><span class="s">&quot;text&quot;</span><span class="p">&gt;&lt;/</span><span class="nt">input</span><span class="p">&gt;&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
<a id="__codelineno-1-38" name="__codelineno-1-38"></a>
<a id="__codelineno-1-39" name="__codelineno-1-39"></a>  <span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;</span>表单事件<span class="p">&lt;/</span><span class="nt">h1</span><span class="p">&gt;</span>
<a id="__codelineno-1-40" name="__codelineno-1-40"></a>  <span class="p">&lt;</span><span class="nt">span</span><span class="p">&gt;</span>当前动作：<span class="p">&lt;/</span><span class="nt">span</span><span class="p">&gt;&lt;</span><span class="nt">span</span> <span class="na">id</span><span class="o">=</span><span class="s">&quot;action_form&quot;</span><span class="p">&gt;&lt;/</span><span class="nt">span</span><span class="p">&gt;</span>
<a id="__codelineno-1-41" name="__codelineno-1-41"></a>  <span class="p">&lt;</span><span class="nt">form</span> <span class="na">id</span><span class="o">=</span><span class="s">&quot;f1&quot;</span><span class="p">&gt;</span>
<a id="__codelineno-1-42" name="__codelineno-1-42"></a>    <span class="p">&lt;</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">&quot;text&quot;</span><span class="p">&gt;&lt;/</span><span class="nt">input</span><span class="p">&gt;</span>
<a id="__codelineno-1-43" name="__codelineno-1-43"></a>    <span class="p">&lt;</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">&quot;submit&quot;</span><span class="p">&gt;&lt;/</span><span class="nt">input</span><span class="p">&gt;</span>
<a id="__codelineno-1-44" name="__codelineno-1-44"></a>  <span class="p">&lt;/</span><span class="nt">form</span><span class="p">&gt;</span>
<a id="__codelineno-1-45" name="__codelineno-1-45"></a>
<a id="__codelineno-1-46" name="__codelineno-1-46"></a>  <span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;</span>文档/窗口事件<span class="p">&lt;/</span><span class="nt">h1</span><span class="p">&gt;</span>
<a id="__codelineno-1-47" name="__codelineno-1-47"></a>  <span class="p">&lt;</span><span class="nt">span</span><span class="p">&gt;</span>当前动作：<span class="p">&lt;/</span><span class="nt">span</span><span class="p">&gt;&lt;</span><span class="nt">span</span> <span class="na">id</span><span class="o">=</span><span class="s">&quot;action_docment&quot;</span><span class="p">&gt;&lt;/</span><span class="nt">span</span><span class="p">&gt;</span>
<a id="__codelineno-1-48" name="__codelineno-1-48"></a><span class="p">&lt;/</span><span class="nt">body</span><span class="p">&gt;</span>
<a id="__codelineno-1-49" name="__codelineno-1-49"></a>
<a id="__codelineno-1-50" name="__codelineno-1-50"></a><span class="p">&lt;/</span><span class="nt">html</span><span class="p">&gt;</span>
</code></pre></div></td></tr></table></div></p>
</li>
<li>
<p>在浏览器中打开页面，操作各模块并观察效果</p>
</li>
</ol>
<h3 id="_3">效果</h3>
<p>创建一个段落元素，通过jQuery分别实现<em>显示/隐藏</em>,<em>淡入/淡出</em>,<em>滑动方法</em>,<em>动画</em>等效果。</p>
<ol>
<li>
<p>编写Html代码，分别实现<em>显示/隐藏</em>,<em>淡入/淡出</em>,<em>滑动方法</em>,<em>动画</em>，参考代码如下：</p>
<p><!--<iframe src="./src/jq_effect.html" frameborder="0" width="100%" height="300"></iframe>-->
<div class="highlight"><table class="highlighttable"><tr><th colspan="2" class="filename"><span class="filename">jq_effect.html</span></th></tr><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"><a href="#__codelineno-2-1"> 1</a></span>
<span class="normal"><a href="#__codelineno-2-2"> 2</a></span>
<span class="normal"><a href="#__codelineno-2-3"> 3</a></span>
<span class="normal"><a href="#__codelineno-2-4"> 4</a></span>
<span class="normal"><a href="#__codelineno-2-5"> 5</a></span>
<span class="normal"><a href="#__codelineno-2-6"> 6</a></span>
<span class="normal"><a href="#__codelineno-2-7"> 7</a></span>
<span class="normal"><a href="#__codelineno-2-8"> 8</a></span>
<span class="normal"><a href="#__codelineno-2-9"> 9</a></span>
<span class="normal"><a href="#__codelineno-2-10">10</a></span>
<span class="normal"><a href="#__codelineno-2-11">11</a></span>
<span class="normal"><a href="#__codelineno-2-12">12</a></span>
<span class="normal"><a href="#__codelineno-2-13">13</a></span>
<span class="normal"><a href="#__codelineno-2-14">14</a></span>
<span class="normal"><a href="#__codelineno-2-15">15</a></span>
<span class="normal"><a href="#__codelineno-2-16">16</a></span>
<span class="normal"><a href="#__codelineno-2-17">17</a></span>
<span class="normal"><a href="#__codelineno-2-18">18</a></span>
<span class="normal"><a href="#__codelineno-2-19">19</a></span>
<span class="normal"><a href="#__codelineno-2-20">20</a></span>
<span class="normal"><a href="#__codelineno-2-21">21</a></span>
<span class="normal"><a href="#__codelineno-2-22">22</a></span>
<span class="normal"><a href="#__codelineno-2-23">23</a></span>
<span class="normal"><a href="#__codelineno-2-24">24</a></span>
<span class="normal"><a href="#__codelineno-2-25">25</a></span>
<span class="normal"><a href="#__codelineno-2-26">26</a></span>
<span class="normal"><a href="#__codelineno-2-27">27</a></span>
<span class="normal"><a href="#__codelineno-2-28">28</a></span>
<span class="normal"><a href="#__codelineno-2-29">29</a></span>
<span class="normal"><a href="#__codelineno-2-30">30</a></span>
<span class="normal"><a href="#__codelineno-2-31">31</a></span>
<span class="normal"><a href="#__codelineno-2-32">32</a></span>
<span class="normal"><a href="#__codelineno-2-33">33</a></span></pre></div></td><td class="code"><div><pre><span></span><code><a id="__codelineno-2-1" name="__codelineno-2-1"></a><span class="cm">&lt;!-- effect.html --&gt;</span>
<a id="__codelineno-2-2" name="__codelineno-2-2"></a><span class="cp">&lt;!DOCTYPE html&gt;</span>
<a id="__codelineno-2-3" name="__codelineno-2-3"></a><span class="p">&lt;</span><span class="nt">html</span><span class="p">&gt;</span>
<a id="__codelineno-2-4" name="__codelineno-2-4"></a>
<a id="__codelineno-2-5" name="__codelineno-2-5"></a><span class="p">&lt;</span><span class="nt">head</span><span class="p">&gt;</span>
<a id="__codelineno-2-6" name="__codelineno-2-6"></a>  <span class="p">&lt;</span><span class="nt">meta</span> <span class="na">charset</span><span class="o">=</span><span class="s">&quot;utf-8&quot;</span><span class="p">&gt;</span>
<a id="__codelineno-2-7" name="__codelineno-2-7"></a>  <span class="p">&lt;</span><span class="nt">title</span><span class="p">&gt;</span>效果示例<span class="p">&lt;/</span><span class="nt">title</span><span class="p">&gt;</span>
<a id="__codelineno-2-8" name="__codelineno-2-8"></a>  <span class="p">&lt;</span><span class="nt">script</span> <span class="na">src</span><span class="o">=</span><span class="s">&quot;jquery-3.6.1.min.js&quot;</span><span class="p">&gt;&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
<a id="__codelineno-2-9" name="__codelineno-2-9"></a><span class="p">&lt;/</span><span class="nt">head</span><span class="p">&gt;</span>
<a id="__codelineno-2-10" name="__codelineno-2-10"></a>
<a id="__codelineno-2-11" name="__codelineno-2-11"></a><span class="p">&lt;</span><span class="nt">body</span><span class="p">&gt;</span>
<a id="__codelineno-2-12" name="__codelineno-2-12"></a>  <span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;</span>这是一个段落。<span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
<a id="__codelineno-2-13" name="__codelineno-2-13"></a>
<a id="__codelineno-2-14" name="__codelineno-2-14"></a>  <span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;</span>显示/隐藏<span class="p">&lt;/</span><span class="nt">h1</span><span class="p">&gt;</span>
<a id="__codelineno-2-15" name="__codelineno-2-15"></a>  <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onclick</span><span class="o">=</span><span class="s">&quot;$(&#39;p&#39;).show()&quot;</span><span class="p">&gt;</span>显示<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
<a id="__codelineno-2-16" name="__codelineno-2-16"></a>  <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onclick</span><span class="o">=</span><span class="s">&quot;$(&#39;p&#39;).hide()&quot;</span><span class="p">&gt;</span>隐藏<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
<a id="__codelineno-2-17" name="__codelineno-2-17"></a>  <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onclick</span><span class="o">=</span><span class="s">&quot;$(&#39;p&#39;).toggle()&quot;</span><span class="p">&gt;</span>隐藏/显示<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
<a id="__codelineno-2-18" name="__codelineno-2-18"></a>
<a id="__codelineno-2-19" name="__codelineno-2-19"></a>  <span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;</span>淡入/淡出<span class="p">&lt;/</span><span class="nt">h1</span><span class="p">&gt;</span>
<a id="__codelineno-2-20" name="__codelineno-2-20"></a>  <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onclick</span><span class="o">=</span><span class="s">&quot;$(&#39;p&#39;).fadeIn()&quot;</span><span class="p">&gt;</span>淡入<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
<a id="__codelineno-2-21" name="__codelineno-2-21"></a>  <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onclick</span><span class="o">=</span><span class="s">&quot;$(&#39;p&#39;).fadeOut()&quot;</span><span class="p">&gt;</span>淡出<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
<a id="__codelineno-2-22" name="__codelineno-2-22"></a>  <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onclick</span><span class="o">=</span><span class="s">&quot;$(&#39;p&#39;).fadeToggle()&quot;</span><span class="p">&gt;</span>淡入/淡出<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
<a id="__codelineno-2-23" name="__codelineno-2-23"></a>
<a id="__codelineno-2-24" name="__codelineno-2-24"></a>  <span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;</span>滑动方法<span class="p">&lt;/</span><span class="nt">h1</span><span class="p">&gt;</span>
<a id="__codelineno-2-25" name="__codelineno-2-25"></a>  <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onclick</span><span class="o">=</span><span class="s">&quot;$(&#39;p&#39;).slideDown()&quot;</span><span class="p">&gt;</span>向下滑动<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
<a id="__codelineno-2-26" name="__codelineno-2-26"></a>  <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onclick</span><span class="o">=</span><span class="s">&quot;$(&#39;p&#39;).slideUp()&quot;</span><span class="p">&gt;</span>向上滑动<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
<a id="__codelineno-2-27" name="__codelineno-2-27"></a>  <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onclick</span><span class="o">=</span><span class="s">&quot;$(&#39;p&#39;).slideToggle()&quot;</span><span class="p">&gt;</span>向下滑动/向上滑动<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
<a id="__codelineno-2-28" name="__codelineno-2-28"></a>
<a id="__codelineno-2-29" name="__codelineno-2-29"></a>  <span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;</span>动画<span class="p">&lt;/</span><span class="nt">h1</span><span class="p">&gt;</span>
<a id="__codelineno-2-30" name="__codelineno-2-30"></a>  <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onclick</span><span class="o">=</span><span class="s">&quot;$(&#39;p&#39;).animate({height:&#39;300px&#39;,opacity:&#39;0.4&#39;}, &#39;slow&#39;)&quot;</span><span class="p">&gt;</span>动画<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
<a id="__codelineno-2-31" name="__codelineno-2-31"></a><span class="p">&lt;/</span><span class="nt">body</span><span class="p">&gt;</span>
<a id="__codelineno-2-32" name="__codelineno-2-32"></a>
<a id="__codelineno-2-33" name="__codelineno-2-33"></a><span class="p">&lt;/</span><span class="nt">html</span><span class="p">&gt;</span>
</code></pre></div></td></tr></table></div></p>
</li>
<li>
<p>在浏览器中打开页面，操作各模块并观察效果</p>
</li>
</ol>
<h3 id="_4">遍历</h3>
<p>编写包含<code>span</code>元素的嵌套对象，使用父节点遍历函数修改<code>span</code>的所有父节点的样式为：<code>{ "color": "red", "border": "2px solid red" }</code>。</p>
<ol>
<li>
<p>编写Html代码，通过jQuery实现<code>span</code>父元素样式修改，参考代码如下：</p>
<p><!--<iframe src="./src/jq_traverse.html" frameborder="0" width="100%" height="300"></iframe>-->
<div class="highlight"><table class="highlighttable"><tr><th colspan="2" class="filename"><span class="filename">jq_traverse.html</span></th></tr><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"><a href="#__codelineno-3-1"> 1</a></span>
<span class="normal"><a href="#__codelineno-3-2"> 2</a></span>
<span class="normal"><a href="#__codelineno-3-3"> 3</a></span>
<span class="normal"><a href="#__codelineno-3-4"> 4</a></span>
<span class="normal"><a href="#__codelineno-3-5"> 5</a></span>
<span class="normal"><a href="#__codelineno-3-6"> 6</a></span>
<span class="normal"><a href="#__codelineno-3-7"> 7</a></span>
<span class="normal"><a href="#__codelineno-3-8"> 8</a></span>
<span class="normal"><a href="#__codelineno-3-9"> 9</a></span>
<span class="normal"><a href="#__codelineno-3-10">10</a></span>
<span class="normal"><a href="#__codelineno-3-11">11</a></span>
<span class="normal"><a href="#__codelineno-3-12">12</a></span>
<span class="normal"><a href="#__codelineno-3-13">13</a></span>
<span class="normal"><a href="#__codelineno-3-14">14</a></span>
<span class="normal"><a href="#__codelineno-3-15">15</a></span>
<span class="normal"><a href="#__codelineno-3-16">16</a></span>
<span class="normal"><a href="#__codelineno-3-17">17</a></span>
<span class="normal"><a href="#__codelineno-3-18">18</a></span>
<span class="normal"><a href="#__codelineno-3-19">19</a></span>
<span class="normal"><a href="#__codelineno-3-20">20</a></span>
<span class="normal"><a href="#__codelineno-3-21">21</a></span>
<span class="normal"><a href="#__codelineno-3-22">22</a></span>
<span class="normal"><a href="#__codelineno-3-23">23</a></span>
<span class="normal"><a href="#__codelineno-3-24">24</a></span>
<span class="normal"><a href="#__codelineno-3-25">25</a></span>
<span class="normal"><a href="#__codelineno-3-26">26</a></span>
<span class="normal"><a href="#__codelineno-3-27">27</a></span>
<span class="normal"><a href="#__codelineno-3-28">28</a></span>
<span class="normal"><a href="#__codelineno-3-29">29</a></span>
<span class="normal"><a href="#__codelineno-3-30">30</a></span>
<span class="normal"><a href="#__codelineno-3-31">31</a></span>
<span class="normal"><a href="#__codelineno-3-32">32</a></span>
<span class="normal"><a href="#__codelineno-3-33">33</a></span>
<span class="normal"><a href="#__codelineno-3-34">34</a></span>
<span class="normal"><a href="#__codelineno-3-35">35</a></span>
<span class="normal"><a href="#__codelineno-3-36">36</a></span>
<span class="normal"><a href="#__codelineno-3-37">37</a></span>
<span class="normal"><a href="#__codelineno-3-38">38</a></span>
<span class="normal"><a href="#__codelineno-3-39">39</a></span>
<span class="normal"><a href="#__codelineno-3-40">40</a></span>
<span class="normal"><a href="#__codelineno-3-41">41</a></span>
<span class="normal"><a href="#__codelineno-3-42">42</a></span>
<span class="normal"><a href="#__codelineno-3-43">43</a></span>
<span class="normal"><a href="#__codelineno-3-44">44</a></span>
<span class="normal"><a href="#__codelineno-3-45">45</a></span>
<span class="normal"><a href="#__codelineno-3-46">46</a></span></pre></div></td><td class="code"><div><pre><span></span><code><a id="__codelineno-3-1" name="__codelineno-3-1"></a><span class="cm">&lt;!-- traverse.html--&gt;</span>
<a id="__codelineno-3-2" name="__codelineno-3-2"></a><span class="cp">&lt;!DOCTYPE html&gt;</span>
<a id="__codelineno-3-3" name="__codelineno-3-3"></a><span class="p">&lt;</span><span class="nt">html</span><span class="p">&gt;</span>
<a id="__codelineno-3-4" name="__codelineno-3-4"></a>
<a id="__codelineno-3-5" name="__codelineno-3-5"></a><span class="p">&lt;</span><span class="nt">head</span><span class="p">&gt;</span>
<a id="__codelineno-3-6" name="__codelineno-3-6"></a>  <span class="p">&lt;</span><span class="nt">meta</span> <span class="na">charset</span><span class="o">=</span><span class="s">&quot;utf-8&quot;</span><span class="p">&gt;</span>
<a id="__codelineno-3-7" name="__codelineno-3-7"></a>  <span class="p">&lt;</span><span class="nt">title</span><span class="p">&gt;</span>遍历示例<span class="p">&lt;/</span><span class="nt">title</span><span class="p">&gt;</span>
<a id="__codelineno-3-8" name="__codelineno-3-8"></a>  <span class="p">&lt;</span><span class="nt">style</span><span class="p">&gt;</span>
<a id="__codelineno-3-9" name="__codelineno-3-9"></a><span class="w">    </span><span class="p">.</span><span class="nc">ancestors</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-3-10" name="__codelineno-3-10"></a><span class="w">      </span><span class="k">display</span><span class="p">:</span><span class="w"> </span><span class="kc">block</span><span class="p">;</span>
<a id="__codelineno-3-11" name="__codelineno-3-11"></a><span class="w">      </span><span class="k">border</span><span class="p">:</span><span class="w"> </span><span class="mi">2</span><span class="kt">px</span><span class="w"> </span><span class="kc">solid</span><span class="w"> </span><span class="kc">lightgrey</span><span class="p">;</span>
<a id="__codelineno-3-12" name="__codelineno-3-12"></a><span class="w">      </span><span class="k">color</span><span class="p">:</span><span class="w"> </span><span class="kc">lightgrey</span><span class="p">;</span>
<a id="__codelineno-3-13" name="__codelineno-3-13"></a><span class="w">      </span><span class="k">padding</span><span class="p">:</span><span class="w"> </span><span class="mi">5</span><span class="kt">px</span><span class="p">;</span>
<a id="__codelineno-3-14" name="__codelineno-3-14"></a><span class="w">      </span><span class="k">margin</span><span class="p">:</span><span class="w"> </span><span class="mi">15</span><span class="kt">px</span><span class="p">;</span>
<a id="__codelineno-3-15" name="__codelineno-3-15"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-3-16" name="__codelineno-3-16"></a><span class="w">  </span><span class="p">&lt;/</span><span class="nt">style</span><span class="p">&gt;</span>
<a id="__codelineno-3-17" name="__codelineno-3-17"></a>  <span class="p">&lt;</span><span class="nt">script</span> <span class="na">src</span><span class="o">=</span><span class="s">&quot;jquery-3.6.1.min.js&quot;</span><span class="p">&gt;</span>
<a id="__codelineno-3-18" name="__codelineno-3-18"></a><span class="w">  </span><span class="p">&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
<a id="__codelineno-3-19" name="__codelineno-3-19"></a>  <span class="p">&lt;</span><span class="nt">script</span><span class="p">&gt;</span>
<a id="__codelineno-3-20" name="__codelineno-3-20"></a><span class="w">    </span><span class="nx">$</span><span class="p">(</span><span class="nb">document</span><span class="p">).</span><span class="nx">ready</span><span class="p">(</span><span class="kd">function</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-3-21" name="__codelineno-3-21"></a><span class="w">      </span><span class="nx">$</span><span class="p">(</span><span class="s2">&quot;span&quot;</span><span class="p">).</span><span class="nx">parent</span><span class="p">().</span><span class="nx">css</span><span class="p">({</span><span class="w"> </span><span class="s2">&quot;color&quot;</span><span class="o">:</span><span class="w"> </span><span class="s2">&quot;red&quot;</span><span class="p">,</span><span class="w"> </span><span class="s2">&quot;border&quot;</span><span class="o">:</span><span class="w"> </span><span class="s2">&quot;2px solid red&quot;</span><span class="w"> </span><span class="p">});</span>
<a id="__codelineno-3-22" name="__codelineno-3-22"></a><span class="w">    </span><span class="p">});</span>
<a id="__codelineno-3-23" name="__codelineno-3-23"></a><span class="w">  </span><span class="p">&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
<a id="__codelineno-3-24" name="__codelineno-3-24"></a><span class="p">&lt;/</span><span class="nt">head</span><span class="p">&gt;</span>
<a id="__codelineno-3-25" name="__codelineno-3-25"></a>
<a id="__codelineno-3-26" name="__codelineno-3-26"></a><span class="p">&lt;</span><span class="nt">body</span><span class="p">&gt;</span>
<a id="__codelineno-3-27" name="__codelineno-3-27"></a>
<a id="__codelineno-3-28" name="__codelineno-3-28"></a>  <span class="p">&lt;</span><span class="nt">div</span> <span class="na">class</span><span class="o">=</span><span class="s">&quot;ancestors&quot;</span><span class="p">&gt;</span>
<a id="__codelineno-3-29" name="__codelineno-3-29"></a>    <span class="p">&lt;</span><span class="nt">div</span> <span class="na">style</span><span class="o">=</span><span class="s">&quot;width:500px;&quot;</span><span class="p">&gt;</span>div (曾祖父元素)
<a id="__codelineno-3-30" name="__codelineno-3-30"></a>      <span class="p">&lt;</span><span class="nt">ul</span><span class="p">&gt;</span>ul (祖父元素)
<a id="__codelineno-3-31" name="__codelineno-3-31"></a>        <span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;</span>li (父元素)
<a id="__codelineno-3-32" name="__codelineno-3-32"></a>          <span class="p">&lt;</span><span class="nt">span</span><span class="p">&gt;</span>span<span class="p">&lt;/</span><span class="nt">span</span><span class="p">&gt;</span>
<a id="__codelineno-3-33" name="__codelineno-3-33"></a>        <span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span>
<a id="__codelineno-3-34" name="__codelineno-3-34"></a>      <span class="p">&lt;/</span><span class="nt">ul</span><span class="p">&gt;</span>
<a id="__codelineno-3-35" name="__codelineno-3-35"></a>    <span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span>
<a id="__codelineno-3-36" name="__codelineno-3-36"></a>
<a id="__codelineno-3-37" name="__codelineno-3-37"></a>    <span class="p">&lt;</span><span class="nt">div</span> <span class="na">style</span><span class="o">=</span><span class="s">&quot;width:500px;&quot;</span><span class="p">&gt;</span>div (祖父元素)
<a id="__codelineno-3-38" name="__codelineno-3-38"></a>      <span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;</span>p (父元素)
<a id="__codelineno-3-39" name="__codelineno-3-39"></a>        <span class="p">&lt;</span><span class="nt">span</span><span class="p">&gt;</span>span<span class="p">&lt;/</span><span class="nt">span</span><span class="p">&gt;</span>
<a id="__codelineno-3-40" name="__codelineno-3-40"></a>      <span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
<a id="__codelineno-3-41" name="__codelineno-3-41"></a>    <span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span>
<a id="__codelineno-3-42" name="__codelineno-3-42"></a>  <span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span>
<a id="__codelineno-3-43" name="__codelineno-3-43"></a>
<a id="__codelineno-3-44" name="__codelineno-3-44"></a><span class="p">&lt;/</span><span class="nt">body</span><span class="p">&gt;</span>
<a id="__codelineno-3-45" name="__codelineno-3-45"></a>
<a id="__codelineno-3-46" name="__codelineno-3-46"></a><span class="p">&lt;/</span><span class="nt">html</span><span class="p">&gt;</span>
</code></pre></div></td></tr></table></div></p>
</li>
<li>
<p>在浏览器中打开页面，操作各模块并观察效果</p>
</li>
</ol>
<h3 id="html">Html</h3>
<p>创建一个段落元素(<code>id=p1</code>)，通过jQuery分别实现对<code>p1</code>的<em>获取内容和属性</em>，<em>设置内容和属性</em>，<em>添加元素</em>，<em>删除元素</em>，<em>CSS类</em>等动作。</p>
<ol>
<li>
<p>编写Html代码，分别实现<em>获取内容和属性</em>，<em>设置内容和属性</em>，<em>添加元素</em>，<em>删除元素</em>，<em>CSS类</em>，参考代码如下：</p>
<p><!--<iframe src="./src/jq_html.html" frameborder="0" width="100%" height="300"></iframe>-->
<div class="highlight"><table class="highlighttable"><tr><th colspan="2" class="filename"><span class="filename">jq_html.html</span></th></tr><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"><a href="#__codelineno-4-1"> 1</a></span>
<span class="normal"><a href="#__codelineno-4-2"> 2</a></span>
<span class="normal"><a href="#__codelineno-4-3"> 3</a></span>
<span class="normal"><a href="#__codelineno-4-4"> 4</a></span>
<span class="normal"><a href="#__codelineno-4-5"> 5</a></span>
<span class="normal"><a href="#__codelineno-4-6"> 6</a></span>
<span class="normal"><a href="#__codelineno-4-7"> 7</a></span>
<span class="normal"><a href="#__codelineno-4-8"> 8</a></span>
<span class="normal"><a href="#__codelineno-4-9"> 9</a></span>
<span class="normal"><a href="#__codelineno-4-10">10</a></span>
<span class="normal"><a href="#__codelineno-4-11">11</a></span>
<span class="normal"><a href="#__codelineno-4-12">12</a></span>
<span class="normal"><a href="#__codelineno-4-13">13</a></span>
<span class="normal"><a href="#__codelineno-4-14">14</a></span>
<span class="normal"><a href="#__codelineno-4-15">15</a></span>
<span class="normal"><a href="#__codelineno-4-16">16</a></span>
<span class="normal"><a href="#__codelineno-4-17">17</a></span>
<span class="normal"><a href="#__codelineno-4-18">18</a></span>
<span class="normal"><a href="#__codelineno-4-19">19</a></span>
<span class="normal"><a href="#__codelineno-4-20">20</a></span>
<span class="normal"><a href="#__codelineno-4-21">21</a></span>
<span class="normal"><a href="#__codelineno-4-22">22</a></span>
<span class="normal"><a href="#__codelineno-4-23">23</a></span>
<span class="normal"><a href="#__codelineno-4-24">24</a></span>
<span class="normal"><a href="#__codelineno-4-25">25</a></span>
<span class="normal"><a href="#__codelineno-4-26">26</a></span>
<span class="normal"><a href="#__codelineno-4-27">27</a></span>
<span class="normal"><a href="#__codelineno-4-28">28</a></span>
<span class="normal"><a href="#__codelineno-4-29">29</a></span>
<span class="normal"><a href="#__codelineno-4-30">30</a></span>
<span class="normal"><a href="#__codelineno-4-31">31</a></span>
<span class="normal"><a href="#__codelineno-4-32">32</a></span>
<span class="normal"><a href="#__codelineno-4-33">33</a></span>
<span class="normal"><a href="#__codelineno-4-34">34</a></span>
<span class="normal"><a href="#__codelineno-4-35">35</a></span>
<span class="normal"><a href="#__codelineno-4-36">36</a></span>
<span class="normal"><a href="#__codelineno-4-37">37</a></span>
<span class="normal"><a href="#__codelineno-4-38">38</a></span>
<span class="normal"><a href="#__codelineno-4-39">39</a></span>
<span class="normal"><a href="#__codelineno-4-40">40</a></span>
<span class="normal"><a href="#__codelineno-4-41">41</a></span>
<span class="normal"><a href="#__codelineno-4-42">42</a></span>
<span class="normal"><a href="#__codelineno-4-43">43</a></span>
<span class="normal"><a href="#__codelineno-4-44">44</a></span>
<span class="normal"><a href="#__codelineno-4-45">45</a></span>
<span class="normal"><a href="#__codelineno-4-46">46</a></span>
<span class="normal"><a href="#__codelineno-4-47">47</a></span>
<span class="normal"><a href="#__codelineno-4-48">48</a></span>
<span class="normal"><a href="#__codelineno-4-49">49</a></span>
<span class="normal"><a href="#__codelineno-4-50">50</a></span>
<span class="normal"><a href="#__codelineno-4-51">51</a></span>
<span class="normal"><a href="#__codelineno-4-52">52</a></span>
<span class="normal"><a href="#__codelineno-4-53">53</a></span>
<span class="normal"><a href="#__codelineno-4-54">54</a></span>
<span class="normal"><a href="#__codelineno-4-55">55</a></span></pre></div></td><td class="code"><div><pre><span></span><code><a id="__codelineno-4-1" name="__codelineno-4-1"></a><span class="cp">&lt;!DOCTYPE html&gt;</span>
<a id="__codelineno-4-2" name="__codelineno-4-2"></a><span class="p">&lt;</span><span class="nt">html</span><span class="p">&gt;</span>
<a id="__codelineno-4-3" name="__codelineno-4-3"></a>
<a id="__codelineno-4-4" name="__codelineno-4-4"></a><span class="p">&lt;</span><span class="nt">head</span><span class="p">&gt;</span>
<a id="__codelineno-4-5" name="__codelineno-4-5"></a>  <span class="p">&lt;</span><span class="nt">meta</span> <span class="na">charset</span><span class="o">=</span><span class="s">&quot;utf-8&quot;</span><span class="p">&gt;</span>
<a id="__codelineno-4-6" name="__codelineno-4-6"></a>  <span class="p">&lt;</span><span class="nt">title</span><span class="p">&gt;</span>Html示例<span class="p">&lt;/</span><span class="nt">title</span><span class="p">&gt;</span>
<a id="__codelineno-4-7" name="__codelineno-4-7"></a>  <span class="p">&lt;</span><span class="nt">script</span> <span class="na">src</span><span class="o">=</span><span class="s">&quot;jquery-3.6.1.min.js&quot;</span><span class="p">&gt;&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
<a id="__codelineno-4-8" name="__codelineno-4-8"></a>  <span class="p">&lt;</span><span class="nt">style</span><span class="p">&gt;</span>
<a id="__codelineno-4-9" name="__codelineno-4-9"></a><span class="w">    </span><span class="p">.</span><span class="nc">ok_class</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-4-10" name="__codelineno-4-10"></a><span class="w">      </span><span class="k">background-color</span><span class="p">:</span><span class="w"> </span><span class="kc">green</span><span class="p">;</span>
<a id="__codelineno-4-11" name="__codelineno-4-11"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-4-12" name="__codelineno-4-12"></a>
<a id="__codelineno-4-13" name="__codelineno-4-13"></a><span class="w">    </span><span class="p">.</span><span class="nc">fail_class</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-4-14" name="__codelineno-4-14"></a><span class="w">      </span><span class="k">background-color</span><span class="p">:</span><span class="w"> </span><span class="kc">red</span><span class="p">;</span>
<a id="__codelineno-4-15" name="__codelineno-4-15"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-4-16" name="__codelineno-4-16"></a>
<a id="__codelineno-4-17" name="__codelineno-4-17"></a><span class="w">    </span><span class="p">.</span><span class="nc">blue</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-4-18" name="__codelineno-4-18"></a><span class="w">      </span><span class="k">color</span><span class="p">:</span><span class="w"> </span><span class="kc">blue</span><span class="p">;</span>
<a id="__codelineno-4-19" name="__codelineno-4-19"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-4-20" name="__codelineno-4-20"></a><span class="w">  </span><span class="p">&lt;/</span><span class="nt">style</span><span class="p">&gt;</span>
<a id="__codelineno-4-21" name="__codelineno-4-21"></a><span class="p">&lt;/</span><span class="nt">head</span><span class="p">&gt;</span>
<a id="__codelineno-4-22" name="__codelineno-4-22"></a>
<a id="__codelineno-4-23" name="__codelineno-4-23"></a><span class="p">&lt;</span><span class="nt">body</span><span class="p">&gt;</span>
<a id="__codelineno-4-24" name="__codelineno-4-24"></a>  <span class="p">&lt;</span><span class="nt">p</span> <span class="na">id</span><span class="o">=</span><span class="s">&quot;p1&quot;</span> <span class="na">class</span><span class="o">=</span><span class="s">&quot;ok_class&quot;</span><span class="p">&gt;</span>这是一个段落。<span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
<a id="__codelineno-4-25" name="__codelineno-4-25"></a>
<a id="__codelineno-4-26" name="__codelineno-4-26"></a>  <span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;</span>获取内容和属性<span class="p">&lt;/</span><span class="nt">h1</span><span class="p">&gt;</span>
<a id="__codelineno-4-27" name="__codelineno-4-27"></a>  <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onclick</span><span class="o">=</span><span class="s">&quot;alert($(&#39;#p1&#39;).text())&quot;</span><span class="p">&gt;</span>text<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
<a id="__codelineno-4-28" name="__codelineno-4-28"></a>  <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onclick</span><span class="o">=</span><span class="s">&quot;alert($(&#39;#p1&#39;).html())&quot;</span><span class="p">&gt;</span>html<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
<a id="__codelineno-4-29" name="__codelineno-4-29"></a>  <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onclick</span><span class="o">=</span><span class="s">&quot;alert($(&#39;#p1&#39;).val())&quot;</span><span class="p">&gt;</span>val<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
<a id="__codelineno-4-30" name="__codelineno-4-30"></a>  <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onclick</span><span class="o">=</span><span class="s">&quot;alert($(&#39;#p1&#39;).attr(&#39;class&#39;))&quot;</span><span class="p">&gt;</span>attr<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
<a id="__codelineno-4-31" name="__codelineno-4-31"></a>
<a id="__codelineno-4-32" name="__codelineno-4-32"></a>  <span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;</span>设置内容和属性<span class="p">&lt;/</span><span class="nt">h1</span><span class="p">&gt;</span>
<a id="__codelineno-4-33" name="__codelineno-4-33"></a>  <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onclick</span><span class="o">=</span><span class="s">&quot;$(&#39;#p1&#39;).text(&#39;hello paragraph!&#39;)&quot;</span><span class="p">&gt;</span>text<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
<a id="__codelineno-4-34" name="__codelineno-4-34"></a>  <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onclick</span><span class="o">=</span><span class="s">&quot;$(&#39;#p1&#39;).text(&#39;&lt;button&gt;hello paragraph!&lt;/button&gt;&#39;)&quot;</span><span class="p">&gt;</span>html<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
<a id="__codelineno-4-35" name="__codelineno-4-35"></a>  <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onclick</span><span class="o">=</span><span class="s">&quot;$(&#39;#p1&#39;).val(&#39;hello val!&#39;)&quot;</span><span class="p">&gt;</span>val<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
<a id="__codelineno-4-36" name="__codelineno-4-36"></a>  <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onclick</span><span class="o">=</span><span class="s">&quot;$(&#39;#p1&#39;).attr(&#39;class&#39;, &#39;fail_class&#39;)&quot;</span><span class="p">&gt;</span>attr<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
<a id="__codelineno-4-37" name="__codelineno-4-37"></a>
<a id="__codelineno-4-38" name="__codelineno-4-38"></a>  <span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;</span>添加元素<span class="p">&lt;/</span><span class="nt">h1</span><span class="p">&gt;</span>
<a id="__codelineno-4-39" name="__codelineno-4-39"></a>  <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onclick</span><span class="o">=</span><span class="s">&quot;$(&#39;#p1&#39;).append(&#39;Some appended text.&#39;)&quot;</span><span class="p">&gt;</span>append<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
<a id="__codelineno-4-40" name="__codelineno-4-40"></a>  <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onclick</span><span class="o">=</span><span class="s">&quot;$(&#39;#p1&#39;).prepend(&#39;Some prepended text.&#39;)&quot;</span><span class="p">&gt;</span>prepended<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
<a id="__codelineno-4-41" name="__codelineno-4-41"></a>  <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onclick</span><span class="o">=</span><span class="s">&quot;$(&#39;#p1&#39;).after(&#39;&lt;p&gt;Some after text.&lt;/p&gt;&#39;)&quot;</span><span class="p">&gt;</span>after<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
<a id="__codelineno-4-42" name="__codelineno-4-42"></a>  <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onclick</span><span class="o">=</span><span class="s">&quot;$(&#39;#p1&#39;).before(&#39;&lt;p&gt;Some before text.&lt;/p&gt;&#39;)&quot;</span><span class="p">&gt;</span>before<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
<a id="__codelineno-4-43" name="__codelineno-4-43"></a>
<a id="__codelineno-4-44" name="__codelineno-4-44"></a>  <span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;</span>删除元素<span class="p">&lt;/</span><span class="nt">h1</span><span class="p">&gt;</span>
<a id="__codelineno-4-45" name="__codelineno-4-45"></a>  <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onclick</span><span class="o">=</span><span class="s">&quot;$(&#39;#p1&#39;).remove()&quot;</span><span class="p">&gt;</span>remove<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
<a id="__codelineno-4-46" name="__codelineno-4-46"></a>  <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onclick</span><span class="o">=</span><span class="s">&quot;$(&#39;#p1&#39;).empty()&quot;</span><span class="p">&gt;</span>remove<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
<a id="__codelineno-4-47" name="__codelineno-4-47"></a>
<a id="__codelineno-4-48" name="__codelineno-4-48"></a>  <span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;</span>CSS类<span class="p">&lt;/</span><span class="nt">h1</span><span class="p">&gt;</span>
<a id="__codelineno-4-49" name="__codelineno-4-49"></a>  <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onclick</span><span class="o">=</span><span class="s">&quot;$(&#39;#p1&#39;).addClass(&#39;blue&#39;)&quot;</span><span class="p">&gt;</span>addClass<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
<a id="__codelineno-4-50" name="__codelineno-4-50"></a>  <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onclick</span><span class="o">=</span><span class="s">&quot;$(&#39;#p1&#39;).removeClass(&#39;blue&#39;)&quot;</span><span class="p">&gt;</span>removeClass<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
<a id="__codelineno-4-51" name="__codelineno-4-51"></a>  <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onclick</span><span class="o">=</span><span class="s">&quot;$(&#39;#p1&#39;).toggleClass(&#39;blue&#39;)&quot;</span><span class="p">&gt;</span>toggleClass<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
<a id="__codelineno-4-52" name="__codelineno-4-52"></a>
<a id="__codelineno-4-53" name="__codelineno-4-53"></a><span class="p">&lt;/</span><span class="nt">body</span><span class="p">&gt;</span>
<a id="__codelineno-4-54" name="__codelineno-4-54"></a>
<a id="__codelineno-4-55" name="__codelineno-4-55"></a><span class="p">&lt;/</span><span class="nt">html</span><span class="p">&gt;</span>
</code></pre></div></td></tr></table></div></p>
</li>
<li>
<p>在浏览器中打开页面，操作各模块并观察效果</p>
</li>
</ol>
<h3 id="ajaxpython">Ajax(Python)</h3>
<p>基于<em>FastAPI</em>创建加(<code>/add</code>)、减(<code>/minus</code>)服务及首页(<code>/</code>)，编写<em>jQuery</em>代码实现服务的<em>Ajax</em>调用。</p>
<ol>
<li>
<p>在工作目录创建<em>ajax</em>子目录并进入</p>
</li>
<li>
<p>创建<code>public</code>子目录并将<code>jquery-3.6.1.min.js</code>放入其中</p>
</li>
<li>
<p>创建首页<code>index.html</code>，实现加、减法服务的<em>Ajax</em>调用，参考代码如下：</p>
<p><!--<iframe src="./src/ajax/py/index.html" frameborder="0" width="100%" height="300"></iframe>-->
<div class="highlight"><table class="highlighttable"><tr><th colspan="2" class="filename"><span class="filename">ajax/py/index.html</span></th></tr><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"><a href="#__codelineno-5-1"> 1</a></span>
<span class="normal"><a href="#__codelineno-5-2"> 2</a></span>
<span class="normal"><a href="#__codelineno-5-3"> 3</a></span>
<span class="normal"><a href="#__codelineno-5-4"> 4</a></span>
<span class="normal"><a href="#__codelineno-5-5"> 5</a></span>
<span class="normal"><a href="#__codelineno-5-6"> 6</a></span>
<span class="normal"><a href="#__codelineno-5-7"> 7</a></span>
<span class="normal"><a href="#__codelineno-5-8"> 8</a></span>
<span class="normal"><a href="#__codelineno-5-9"> 9</a></span>
<span class="normal"><a href="#__codelineno-5-10">10</a></span>
<span class="normal"><a href="#__codelineno-5-11">11</a></span>
<span class="normal"><a href="#__codelineno-5-12">12</a></span>
<span class="normal"><a href="#__codelineno-5-13">13</a></span>
<span class="normal"><a href="#__codelineno-5-14">14</a></span>
<span class="normal"><a href="#__codelineno-5-15">15</a></span>
<span class="normal"><a href="#__codelineno-5-16">16</a></span>
<span class="normal"><a href="#__codelineno-5-17">17</a></span>
<span class="normal"><a href="#__codelineno-5-18">18</a></span>
<span class="normal"><a href="#__codelineno-5-19">19</a></span>
<span class="normal"><a href="#__codelineno-5-20">20</a></span>
<span class="normal"><a href="#__codelineno-5-21">21</a></span>
<span class="normal"><a href="#__codelineno-5-22">22</a></span>
<span class="normal"><a href="#__codelineno-5-23">23</a></span>
<span class="normal"><a href="#__codelineno-5-24">24</a></span>
<span class="normal"><a href="#__codelineno-5-25">25</a></span>
<span class="normal"><a href="#__codelineno-5-26">26</a></span>
<span class="normal"><a href="#__codelineno-5-27">27</a></span>
<span class="normal"><a href="#__codelineno-5-28">28</a></span>
<span class="normal"><a href="#__codelineno-5-29">29</a></span>
<span class="normal"><a href="#__codelineno-5-30">30</a></span>
<span class="normal"><a href="#__codelineno-5-31">31</a></span>
<span class="normal"><a href="#__codelineno-5-32">32</a></span>
<span class="normal"><a href="#__codelineno-5-33">33</a></span>
<span class="normal"><a href="#__codelineno-5-34">34</a></span>
<span class="normal"><a href="#__codelineno-5-35">35</a></span>
<span class="normal"><a href="#__codelineno-5-36">36</a></span>
<span class="normal"><a href="#__codelineno-5-37">37</a></span>
<span class="normal"><a href="#__codelineno-5-38">38</a></span>
<span class="normal"><a href="#__codelineno-5-39">39</a></span>
<span class="normal"><a href="#__codelineno-5-40">40</a></span>
<span class="normal"><a href="#__codelineno-5-41">41</a></span>
<span class="normal"><a href="#__codelineno-5-42">42</a></span>
<span class="normal"><a href="#__codelineno-5-43">43</a></span>
<span class="normal"><a href="#__codelineno-5-44">44</a></span>
<span class="normal"><a href="#__codelineno-5-45">45</a></span>
<span class="normal"><a href="#__codelineno-5-46">46</a></span>
<span class="normal"><a href="#__codelineno-5-47">47</a></span>
<span class="normal"><a href="#__codelineno-5-48">48</a></span>
<span class="normal"><a href="#__codelineno-5-49">49</a></span>
<span class="normal"><a href="#__codelineno-5-50">50</a></span>
<span class="normal"><a href="#__codelineno-5-51">51</a></span>
<span class="normal"><a href="#__codelineno-5-52">52</a></span>
<span class="normal"><a href="#__codelineno-5-53">53</a></span>
<span class="normal"><a href="#__codelineno-5-54">54</a></span>
<span class="normal"><a href="#__codelineno-5-55">55</a></span></pre></div></td><td class="code"><div><pre><span></span><code><a id="__codelineno-5-1" name="__codelineno-5-1"></a><span class="cm">&lt;!-- index.html --&gt;</span>
<a id="__codelineno-5-2" name="__codelineno-5-2"></a><span class="cp">&lt;!DOCTYPE html&gt;</span>
<a id="__codelineno-5-3" name="__codelineno-5-3"></a><span class="p">&lt;</span><span class="nt">html</span><span class="p">&gt;</span>
<a id="__codelineno-5-4" name="__codelineno-5-4"></a>
<a id="__codelineno-5-5" name="__codelineno-5-5"></a><span class="p">&lt;</span><span class="nt">head</span><span class="p">&gt;</span>
<a id="__codelineno-5-6" name="__codelineno-5-6"></a>  <span class="p">&lt;</span><span class="nt">meta</span> <span class="na">charset</span><span class="o">=</span><span class="s">&quot;utf-8&quot;</span><span class="p">&gt;</span>
<a id="__codelineno-5-7" name="__codelineno-5-7"></a>  <span class="p">&lt;</span><span class="nt">title</span><span class="p">&gt;</span>Ajax示例<span class="p">&lt;/</span><span class="nt">title</span><span class="p">&gt;</span>
<a id="__codelineno-5-8" name="__codelineno-5-8"></a>  <span class="p">&lt;</span><span class="nt">meta</span> <span class="na">http-equiv</span><span class="o">=</span><span class="s">&quot;Access-Control-Allow-Origin&quot;</span> <span class="na">content</span><span class="o">=</span><span class="s">&quot;*&quot;</span><span class="p">&gt;</span>
<a id="__codelineno-5-9" name="__codelineno-5-9"></a>  <span class="p">&lt;</span><span class="nt">script</span> <span class="na">src</span><span class="o">=</span><span class="s">&quot;public/jquery-3.6.1.min.js&quot;</span><span class="p">&gt;</span>
<a id="__codelineno-5-10" name="__codelineno-5-10"></a><span class="w">  </span><span class="p">&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
<a id="__codelineno-5-11" name="__codelineno-5-11"></a>  <span class="p">&lt;</span><span class="nt">script</span><span class="p">&gt;</span>
<a id="__codelineno-5-12" name="__codelineno-5-12"></a><span class="w">    </span><span class="nx">$</span><span class="p">(</span><span class="nb">document</span><span class="p">).</span><span class="nx">ready</span><span class="p">(</span><span class="kd">function</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-5-13" name="__codelineno-5-13"></a><span class="w">      </span><span class="nx">$</span><span class="p">(</span><span class="s2">&quot;#add&quot;</span><span class="p">).</span><span class="nx">click</span><span class="p">(</span><span class="kd">function</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-5-14" name="__codelineno-5-14"></a><span class="w">        </span><span class="kd">const</span><span class="w"> </span><span class="nx">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">parseFloat</span><span class="p">(</span><span class="nx">$</span><span class="p">(</span><span class="s2">&quot;#a&quot;</span><span class="p">).</span><span class="nx">val</span><span class="p">());</span>
<a id="__codelineno-5-15" name="__codelineno-5-15"></a><span class="w">        </span><span class="kd">const</span><span class="w"> </span><span class="nx">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">parseFloat</span><span class="p">(</span><span class="nx">$</span><span class="p">(</span><span class="s2">&quot;#b&quot;</span><span class="p">).</span><span class="nx">val</span><span class="p">());</span>
<a id="__codelineno-5-16" name="__codelineno-5-16"></a><span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="o">!</span><span class="nb">isNaN</span><span class="p">(</span><span class="nx">a</span><span class="p">)</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="o">!</span><span class="nb">isNaN</span><span class="p">(</span><span class="nx">b</span><span class="p">))</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-5-17" name="__codelineno-5-17"></a><span class="w">          </span><span class="nx">$</span><span class="p">.</span><span class="nx">ajax</span><span class="p">({</span>
<a id="__codelineno-5-18" name="__codelineno-5-18"></a><span class="w">            </span><span class="nx">type</span><span class="o">:</span><span class="w"> </span><span class="s2">&quot;POST&quot;</span><span class="p">,</span>
<a id="__codelineno-5-19" name="__codelineno-5-19"></a><span class="w">            </span><span class="nx">url</span><span class="o">:</span><span class="w"> </span><span class="s2">&quot;/add&quot;</span><span class="p">,</span>
<a id="__codelineno-5-20" name="__codelineno-5-20"></a><span class="w">            </span><span class="nx">contentType</span><span class="o">:</span><span class="w"> </span><span class="s2">&quot;application/json; charset=utf-8&quot;</span><span class="p">,</span>
<a id="__codelineno-5-21" name="__codelineno-5-21"></a><span class="w">            </span><span class="nx">data</span><span class="o">:</span><span class="w"> </span><span class="nb">JSON</span><span class="p">.</span><span class="nx">stringify</span><span class="p">({</span><span class="w"> </span><span class="nx">a</span><span class="o">:</span><span class="w"> </span><span class="nx">a</span><span class="p">,</span><span class="w"> </span><span class="nx">b</span><span class="o">:</span><span class="w"> </span><span class="nx">b</span><span class="w"> </span><span class="p">}),</span>
<a id="__codelineno-5-22" name="__codelineno-5-22"></a><span class="w">            </span><span class="nx">success</span><span class="o">:</span><span class="w"> </span><span class="kd">function</span><span class="w"> </span><span class="p">(</span><span class="nx">res</span><span class="p">,</span><span class="w"> </span><span class="nx">status</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-5-23" name="__codelineno-5-23"></a><span class="w">              </span><span class="nx">$</span><span class="p">(</span><span class="s2">&quot;#a_b&quot;</span><span class="p">).</span><span class="nx">val</span><span class="p">(</span><span class="nx">res</span><span class="p">);</span>
<a id="__codelineno-5-24" name="__codelineno-5-24"></a><span class="w">            </span><span class="p">},</span>
<a id="__codelineno-5-25" name="__codelineno-5-25"></a><span class="w">            </span><span class="nx">error</span><span class="o">:</span><span class="w"> </span><span class="kd">function</span><span class="w"> </span><span class="p">(</span><span class="nx">message</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-5-26" name="__codelineno-5-26"></a><span class="w">              </span><span class="nx">$</span><span class="p">(</span><span class="s2">&quot;#a_b&quot;</span><span class="p">).</span><span class="nx">val</span><span class="p">(</span><span class="nx">message</span><span class="p">);</span>
<a id="__codelineno-5-27" name="__codelineno-5-27"></a><span class="w">            </span><span class="p">}</span>
<a id="__codelineno-5-28" name="__codelineno-5-28"></a><span class="w">          </span><span class="p">})</span>
<a id="__codelineno-5-29" name="__codelineno-5-29"></a><span class="w">        </span><span class="p">}</span>
<a id="__codelineno-5-30" name="__codelineno-5-30"></a><span class="w">      </span><span class="p">});</span>
<a id="__codelineno-5-31" name="__codelineno-5-31"></a>
<a id="__codelineno-5-32" name="__codelineno-5-32"></a><span class="w">      </span><span class="nx">$</span><span class="p">(</span><span class="s2">&quot;#minus&quot;</span><span class="p">).</span><span class="nx">click</span><span class="p">(</span><span class="kd">function</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-5-33" name="__codelineno-5-33"></a><span class="w">        </span><span class="kd">const</span><span class="w"> </span><span class="nx">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">parseFloat</span><span class="p">(</span><span class="nx">$</span><span class="p">(</span><span class="s2">&quot;#a&quot;</span><span class="p">).</span><span class="nx">val</span><span class="p">());</span>
<a id="__codelineno-5-34" name="__codelineno-5-34"></a><span class="w">        </span><span class="kd">const</span><span class="w"> </span><span class="nx">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">parseFloat</span><span class="p">(</span><span class="nx">$</span><span class="p">(</span><span class="s2">&quot;#b&quot;</span><span class="p">).</span><span class="nx">val</span><span class="p">());</span>
<a id="__codelineno-5-35" name="__codelineno-5-35"></a><span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="o">!</span><span class="nb">isNaN</span><span class="p">(</span><span class="nx">a</span><span class="p">)</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="o">!</span><span class="nb">isNaN</span><span class="p">(</span><span class="nx">b</span><span class="p">))</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-5-36" name="__codelineno-5-36"></a><span class="w">          </span><span class="nx">$</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s2">&quot;/minus&quot;</span><span class="p">,</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">a</span><span class="o">:</span><span class="w"> </span><span class="nx">a</span><span class="p">,</span><span class="w"> </span><span class="nx">b</span><span class="o">:</span><span class="w"> </span><span class="nx">b</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="kd">function</span><span class="w"> </span><span class="p">(</span><span class="nx">res</span><span class="p">,</span><span class="w"> </span><span class="nx">status</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-5-37" name="__codelineno-5-37"></a><span class="w">            </span><span class="nx">$</span><span class="p">(</span><span class="s2">&quot;#a_b&quot;</span><span class="p">).</span><span class="nx">val</span><span class="p">(</span><span class="nx">res</span><span class="p">);</span>
<a id="__codelineno-5-38" name="__codelineno-5-38"></a><span class="w">          </span><span class="p">})</span>
<a id="__codelineno-5-39" name="__codelineno-5-39"></a><span class="w">        </span><span class="p">}</span>
<a id="__codelineno-5-40" name="__codelineno-5-40"></a><span class="w">      </span><span class="p">});</span>
<a id="__codelineno-5-41" name="__codelineno-5-41"></a><span class="w">    </span><span class="p">});</span>
<a id="__codelineno-5-42" name="__codelineno-5-42"></a><span class="w">  </span><span class="p">&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
<a id="__codelineno-5-43" name="__codelineno-5-43"></a><span class="p">&lt;/</span><span class="nt">head</span><span class="p">&gt;</span>
<a id="__codelineno-5-44" name="__codelineno-5-44"></a>
<a id="__codelineno-5-45" name="__codelineno-5-45"></a><span class="p">&lt;</span><span class="nt">body</span><span class="p">&gt;</span>
<a id="__codelineno-5-46" name="__codelineno-5-46"></a>  <span class="p">&lt;</span><span class="nt">span</span><span class="p">&gt;</span>a：<span class="p">&lt;/</span><span class="nt">span</span><span class="p">&gt;</span>
<a id="__codelineno-5-47" name="__codelineno-5-47"></a>  <span class="p">&lt;</span><span class="nt">input</span> <span class="na">id</span><span class="o">=</span><span class="s">&quot;a&quot;</span> <span class="na">type</span><span class="o">=</span><span class="s">&quot;text&quot;</span> <span class="p">/&gt;</span>
<a id="__codelineno-5-48" name="__codelineno-5-48"></a>  <span class="p">&lt;</span><span class="nt">span</span><span class="p">&gt;</span>b：<span class="p">&lt;/</span><span class="nt">span</span><span class="p">&gt;</span>
<a id="__codelineno-5-49" name="__codelineno-5-49"></a>  <span class="p">&lt;</span><span class="nt">input</span> <span class="na">id</span><span class="o">=</span><span class="s">&quot;b&quot;</span> <span class="na">type</span><span class="o">=</span><span class="s">&quot;text&quot;</span> <span class="p">/&gt;</span>
<a id="__codelineno-5-50" name="__codelineno-5-50"></a>  <span class="p">&lt;</span><span class="nt">button</span> <span class="na">id</span><span class="o">=</span><span class="s">&quot;add&quot;</span><span class="p">&gt;</span>a+b<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
<a id="__codelineno-5-51" name="__codelineno-5-51"></a>  <span class="p">&lt;</span><span class="nt">button</span> <span class="na">id</span><span class="o">=</span><span class="s">&quot;minus&quot;</span><span class="p">&gt;</span>a-b<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
<a id="__codelineno-5-52" name="__codelineno-5-52"></a>  <span class="p">&lt;</span><span class="nt">input</span> <span class="na">id</span><span class="o">=</span><span class="s">&quot;a_b&quot;</span> <span class="na">type</span><span class="o">=</span><span class="s">&quot;text&quot;</span> <span class="p">/&gt;</span>
<a id="__codelineno-5-53" name="__codelineno-5-53"></a><span class="p">&lt;/</span><span class="nt">body</span><span class="p">&gt;</span>
<a id="__codelineno-5-54" name="__codelineno-5-54"></a>
<a id="__codelineno-5-55" name="__codelineno-5-55"></a><span class="p">&lt;/</span><span class="nt">html</span><span class="p">&gt;</span>
</code></pre></div></td></tr></table></div></p>
</li>
<li>
<p>创建<code>my_server.py</code>实现基于<em>FastApi</em>的加(<code>/add</code>)、减(<code>/minus</code>)服务及首页(<code>/</code>)，参考代码如下：</p>
<p><!--<iframe src="./src/ajax/py/my_server.py" frameborder="0" width="100%" height="300"></iframe>-->
<div class="highlight"><table class="highlighttable"><tr><th colspan="2" class="filename"><span class="filename">ajax/py/my_server.py</span></th></tr><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"><a href="#__codelineno-6-1"> 1</a></span>
<span class="normal"><a href="#__codelineno-6-2"> 2</a></span>
<span class="normal"><a href="#__codelineno-6-3"> 3</a></span>
<span class="normal"><a href="#__codelineno-6-4"> 4</a></span>
<span class="normal"><a href="#__codelineno-6-5"> 5</a></span>
<span class="normal"><a href="#__codelineno-6-6"> 6</a></span>
<span class="normal"><a href="#__codelineno-6-7"> 7</a></span>
<span class="normal"><a href="#__codelineno-6-8"> 8</a></span>
<span class="normal"><a href="#__codelineno-6-9"> 9</a></span>
<span class="normal"><a href="#__codelineno-6-10">10</a></span>
<span class="normal"><a href="#__codelineno-6-11">11</a></span>
<span class="normal"><a href="#__codelineno-6-12">12</a></span>
<span class="normal"><a href="#__codelineno-6-13">13</a></span>
<span class="normal"><a href="#__codelineno-6-14">14</a></span>
<span class="normal"><a href="#__codelineno-6-15">15</a></span>
<span class="normal"><a href="#__codelineno-6-16">16</a></span>
<span class="normal"><a href="#__codelineno-6-17">17</a></span>
<span class="normal"><a href="#__codelineno-6-18">18</a></span>
<span class="normal"><a href="#__codelineno-6-19">19</a></span>
<span class="normal"><a href="#__codelineno-6-20">20</a></span>
<span class="normal"><a href="#__codelineno-6-21">21</a></span>
<span class="normal"><a href="#__codelineno-6-22">22</a></span>
<span class="normal"><a href="#__codelineno-6-23">23</a></span>
<span class="normal"><a href="#__codelineno-6-24">24</a></span>
<span class="normal"><a href="#__codelineno-6-25">25</a></span>
<span class="normal"><a href="#__codelineno-6-26">26</a></span>
<span class="normal"><a href="#__codelineno-6-27">27</a></span>
<span class="normal"><a href="#__codelineno-6-28">28</a></span>
<span class="normal"><a href="#__codelineno-6-29">29</a></span>
<span class="normal"><a href="#__codelineno-6-30">30</a></span>
<span class="normal"><a href="#__codelineno-6-31">31</a></span>
<span class="normal"><a href="#__codelineno-6-32">32</a></span>
<span class="normal"><a href="#__codelineno-6-33">33</a></span>
<span class="normal"><a href="#__codelineno-6-34">34</a></span>
<span class="normal"><a href="#__codelineno-6-35">35</a></span>
<span class="normal"><a href="#__codelineno-6-36">36</a></span>
<span class="normal"><a href="#__codelineno-6-37">37</a></span>
<span class="normal"><a href="#__codelineno-6-38">38</a></span>
<span class="normal"><a href="#__codelineno-6-39">39</a></span>
<span class="normal"><a href="#__codelineno-6-40">40</a></span>
<span class="normal"><a href="#__codelineno-6-41">41</a></span>
<span class="normal"><a href="#__codelineno-6-42">42</a></span>
<span class="normal"><a href="#__codelineno-6-43">43</a></span>
<span class="normal"><a href="#__codelineno-6-44">44</a></span>
<span class="normal"><a href="#__codelineno-6-45">45</a></span>
<span class="normal"><a href="#__codelineno-6-46">46</a></span>
<span class="normal"><a href="#__codelineno-6-47">47</a></span>
<span class="normal"><a href="#__codelineno-6-48">48</a></span>
<span class="normal"><a href="#__codelineno-6-49">49</a></span></pre></div></td><td class="code"><div><pre><span></span><code><a id="__codelineno-6-1" name="__codelineno-6-1"></a># -*- coding:utf-8 -*-
<a id="__codelineno-6-2" name="__codelineno-6-2"></a># my_server.py
<a id="__codelineno-6-3" name="__codelineno-6-3"></a>import logging
<a id="__codelineno-6-4" name="__codelineno-6-4"></a>from fastapi import FastAPI
<a id="__codelineno-6-5" name="__codelineno-6-5"></a>from fastapi.responses import HTMLResponse
<a id="__codelineno-6-6" name="__codelineno-6-6"></a>from fastapi.staticfiles import StaticFiles
<a id="__codelineno-6-7" name="__codelineno-6-7"></a>from pydantic import BaseModel
<a id="__codelineno-6-8" name="__codelineno-6-8"></a>import uvicorn
<a id="__codelineno-6-9" name="__codelineno-6-9"></a>
<a id="__codelineno-6-10" name="__codelineno-6-10"></a>log = logging.getLogger(&quot;my_server&quot;)
<a id="__codelineno-6-11" name="__codelineno-6-11"></a>app = FastAPI()
<a id="__codelineno-6-12" name="__codelineno-6-12"></a>app.mount(&quot;/public&quot;, StaticFiles(directory=&quot;public&quot;), &quot;public&quot;)
<a id="__codelineno-6-13" name="__codelineno-6-13"></a>
<a id="__codelineno-6-14" name="__codelineno-6-14"></a>@app.get(&quot;/&quot;, response_class=HTMLResponse)
<a id="__codelineno-6-15" name="__codelineno-6-15"></a>async def root():
<a id="__codelineno-6-16" name="__codelineno-6-16"></a>  &#39;&#39;&#39;首页&#39;&#39;&#39;
<a id="__codelineno-6-17" name="__codelineno-6-17"></a>  html_file = open(file=&quot;index.html&quot;, mode=&quot;r&quot;, encoding=&quot;utf-8&quot;).read()
<a id="__codelineno-6-18" name="__codelineno-6-18"></a>  return html_file
<a id="__codelineno-6-19" name="__codelineno-6-19"></a>
<a id="__codelineno-6-20" name="__codelineno-6-20"></a>class AddInput(BaseModel):
<a id="__codelineno-6-21" name="__codelineno-6-21"></a>  &#39;&#39;&#39;加法输入&#39;&#39;&#39;
<a id="__codelineno-6-22" name="__codelineno-6-22"></a>  a: float = 0
<a id="__codelineno-6-23" name="__codelineno-6-23"></a>  b: float = 2
<a id="__codelineno-6-24" name="__codelineno-6-24"></a>
<a id="__codelineno-6-25" name="__codelineno-6-25"></a>@app.post(&quot;/add&quot;)
<a id="__codelineno-6-26" name="__codelineno-6-26"></a>async def add(v: AddInput):
<a id="__codelineno-6-27" name="__codelineno-6-27"></a>  &#39;&#39;&#39;加法服务&#39;&#39;&#39;
<a id="__codelineno-6-28" name="__codelineno-6-28"></a>  log.debug(&quot;input:{},{}&quot;.format(v.a, v.b))
<a id="__codelineno-6-29" name="__codelineno-6-29"></a>  return v.a + v.b
<a id="__codelineno-6-30" name="__codelineno-6-30"></a>
<a id="__codelineno-6-31" name="__codelineno-6-31"></a>@app.get(&quot;/minus&quot;)
<a id="__codelineno-6-32" name="__codelineno-6-32"></a>async def minus(a:float, b:float):
<a id="__codelineno-6-33" name="__codelineno-6-33"></a>  &#39;&#39;&#39;
<a id="__codelineno-6-34" name="__codelineno-6-34"></a>  减法服务
<a id="__codelineno-6-35" name="__codelineno-6-35"></a>  @param a: 操作数1
<a id="__codelineno-6-36" name="__codelineno-6-36"></a>  @param b: 操作数2
<a id="__codelineno-6-37" name="__codelineno-6-37"></a>  &#39;&#39;&#39;
<a id="__codelineno-6-38" name="__codelineno-6-38"></a>  log.debug(&quot;input:{},{}&quot;.format(a, b))
<a id="__codelineno-6-39" name="__codelineno-6-39"></a>  return a - b
<a id="__codelineno-6-40" name="__codelineno-6-40"></a>
<a id="__codelineno-6-41" name="__codelineno-6-41"></a>def start(port: int = 8888):
<a id="__codelineno-6-42" name="__codelineno-6-42"></a>  &#39;&#39;&#39;启动FastAPI服务器&#39;&#39;&#39;
<a id="__codelineno-6-43" name="__codelineno-6-43"></a>  config = uvicorn.Config(app = app, port = port, log_level = &quot;info&quot;, reload = False)
<a id="__codelineno-6-44" name="__codelineno-6-44"></a>  server = uvicorn.Server(config)
<a id="__codelineno-6-45" name="__codelineno-6-45"></a>  server.run()
<a id="__codelineno-6-46" name="__codelineno-6-46"></a>
<a id="__codelineno-6-47" name="__codelineno-6-47"></a>if __name__ == &quot;__main__&quot;:
<a id="__codelineno-6-48" name="__codelineno-6-48"></a>  logging.basicConfig(level=logging.DEBUG)
<a id="__codelineno-6-49" name="__codelineno-6-49"></a>  start()
</code></pre></div></td></tr></table></div></p>
</li>
<li>
<p>启动服务器</p>
<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"><a href="#__codelineno-7-1">1</a></span></pre></div></td><td class="code"><div><pre><span></span><code><a id="__codelineno-7-1" name="__codelineno-7-1"></a>python<span class="w"> </span>my_server.py
</code></pre></div></td></tr></table></div>
</li>
<li>
<p>在浏览器主访问<code>http://127.0.0.1:8888/</code>并通过界面提交加、减法服务调用请求</p>
</li>
</ol>





                
              </article>
            </div>
          
          
  <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var tab,labels=set.querySelector(".tabbed-labels");for(tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script>

        </div>
        
      </main>
      
        <footer class="md-footer">
  
  <div class="md-footer-meta md-typeset">
    <div class="md-footer-meta__inner md-grid">
      <div class="md-copyright">
  
  
    Made with
    <a href="https://squidfunk.github.io/mkdocs-material/" target="_blank" rel="noopener">
      Material for MkDocs
    </a>
  
</div>
      
    </div>
  </div>
</footer>
      
    </div>
    <div class="md-dialog" data-md-component="dialog">
      <div class="md-dialog__inner md-typeset"></div>
    </div>
    
    <script id="__config" type="application/json">{"base": "..", "features": ["content.tabs.link", "navigation.expand", "navigation.sections", "navigation.path"], "search": "../assets/javascripts/workers/search.208ed371.min.js", "translations": {"clipboard.copied": "Copied to clipboard", "clipboard.copy": "Copy to clipboard", "search.result.more.one": "1 more on this page", "search.result.more.other": "# more on this page", "search.result.none": "No matching documents", "search.result.one": "1 matching document", "search.result.other": "# matching documents", "search.result.placeholder": "Type to start searching", "search.result.term.missing": "Missing", "select.version": "Select version"}}</script>
    
    
      <script src="../assets/javascripts/bundle.efa0ade1.min.js"></script>
      
    
  </body>
</html>